//
//  ScanBigImage.m
//  cn.zhikao.com
//
//  Created by WJ on 2017/11/22.
//  Copyright © 2017年 WJ. All rights reserved.
//

#import "ScanBigImage.h"

@implementation ScanBigImage

static CGRect originalFrame;


+ (void)scanBigImageWithImageView:(UIImageView *)currentImageView alpha:(CGFloat)alpha {
    
    UIImage *image = currentImageView.image;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];


    originalFrame = [currentImageView convertRect:currentImageView.bounds toView:window];
    [backGroundView setBackgroundColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:alpha]];
    [backGroundView setAlpha:0];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:originalFrame];
    [imageView setImage:image];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [imageView setTag:1024];
    [backGroundView addSubview:imageView];
    [window addSubview:backGroundView];

    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
    [backGroundView addGestureRecognizer:tap];
    
    [UIView animateWithDuration:0.4 animations:^{
        CGFloat y,width,height;
        y = ([UIScreen mainScreen].bounds.size.height - image.size.height *[UIScreen mainScreen].bounds.size.width / image.size.width ) * 0.5;
        width = [UIScreen mainScreen].bounds.size.width;
        height = image.size.height *[UIScreen mainScreen].bounds.size.width / image.size.width;
        [imageView setFrame:CGRectMake(0, y, width, height)];
        [backGroundView setAlpha:1];
    } completion:^(BOOL finished) {
        
    }];
}

+ (void)hideImageView:(UITapGestureRecognizer *) tap {
    UIView *backgroundView = tap.view;
    //  原始imageview
    UIImageView *imageView = [tap.view viewWithTag:1024];
    //  恢复
    [UIView animateWithDuration:0.4 animations:^{
        [imageView setFrame:originalFrame];
        [backgroundView setAlpha:0];
    } completion:^(BOOL finished) {
        //完成后操作->将背景视图删掉
        [backgroundView removeFromSuperview];
    }];
    
}
@end
